package com.otaliastudios.cameraview.demo

import android.graphics.Color
import android.graphics.Point
import android.os.Bundle
import android.os.Environment
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.otaliastudios.cameraview.*
import com.otaliastudios.cameraview.entity.WarterMarkBean
import com.otaliastudios.cameraview.livedata.MarkMapLiveData
import com.otaliastudios.cameraview.entity.MarkGravity
import com.otaliastudios.cameraview.livedata.MarkSizeLiveData
import java.io.File
import java.text.SimpleDateFormat
import java.util.*


class AHDActivity : AppCompatActivity() {
    lateinit var cameraView: CameraView
    val saveVideoPath: String
        get() {
            val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val time = format.format(Date())
            return Environment.getExternalStorageDirectory().path + "/test/live_save_video-" + time + ".mp4"
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ahdactivity)
        cameraView = findViewById<CameraView>(R.id.cameraView)
        findViewById<MarkView>(R.id.markview).setLifecycleOwner(this)
        cameraView.setLifecycleOwner(this)

        cameraView.addCameraListener(object : CameraListener() {
            override fun equals(other: Any?): Boolean {
                return super.equals(other)
            }

            override fun onPictureTaken(result: PictureResult) {
                super.onPictureTaken(result)
                LogPlus.w("图片尺寸===>${result.size}--->角度-->${result.rotation}")
                result.toBitmap {
                    findViewById<ImageView>(R.id.photoImage).setImageBitmap(it)

                }
            }

            override fun onVideoTaken(result: VideoResult) {
                super.onVideoTaken(result)
                LogPlus.e("录像路径==>${result.file.name}")
            }

            override fun onVideoRecordingEnd() {
                super.onVideoRecordingEnd()
            }
        })

        findViewById<Button>(R.id.takephoto).setOnClickListener {
            cameraView.takePictureSnapshot()
        }
        findViewById<Button>(R.id.takeVideo).setOnClickListener {
            if (cameraView.isTakingVideo) cameraView.stopVideo()
            else {
                val file = File(saveVideoPath)
                file.parentFile.mkdirs()
                cameraView.takeVideoSnapshot(file)
            }
        }

        var value = MarkMapLiveData.get().value

        val size = MarkSizeLiveData.get().value
        value["ooo"] = WarterMarkBean("ooo", Point(32, 32), null, "我来了,我日了狗啊", Color.WHITE)
        value["TTT"] = WarterMarkBean("TTT", Point(32, 32 * 2), null, "狗中大长腿柯基", Color.WHITE)
        value["UUU"] = WarterMarkBean(
            "UUU",
            Point(size!!.width, size!!.height-100),
            null,
            "柯基是大长腿",
            Color.WHITE,
            32F,
            MarkGravity.RIGHT, -180
        )
    }
}